package controllers
{
	import events.*;
	
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import models.Model;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import spark.components.Application;

	public class Controller
	{
		public var appModel:Model;
		public var parentApplication:Application;
		public static const KEY:String="6b10ad4fd0a1033353039876a994ed72";
		private var state:int = ImageManipulationState.NONE;
		private var oldImageProperty:Vector3D = new Vector3D(0,0,0);

		
		public var flickrService:HTTPService;
		
		public function Controller()			
		{}
		
		public function moveFocus(event:MoveFocusEvent):void
		{
			if (event.direction == "left" )
				appModel.moveToPreviousSlide();
			else
				appModel.moveToNextSlide();				
		}	
		public function ChangeState(event:ChangeStatesEvent):void
		{			
			appModel.resetSlideShow();
			if (event.state == parentApplication.states[1])
			{

				parentApplication.currentState = event.state;

			}
			else
			{
				parentApplication.currentState = event.state;
			}
		}		
		
		public function DeleteSlide(event:DeleteSlideEvent):void
		{
			appModel.deleteCurrentSlide();
		}
		
		public function Search(event:SearchEvent):void
		{							
			var params:Object = {};
			params.api_key = KEY;
			params.text = event.searchText;
			flickrService.send(params);		

		}
		public function faultHandler(result:FaultEvent):void
		{
			Alert.show("Connection Error!");
		}
		public function resultHandler(result:ResultEvent):void
		{
			var photos:ArrayCollection =  result.result.rsp.photos.photo;
			var flickrImages:ArrayCollection = new ArrayCollection();
			if(photos.length>0)
			{
			
				var p:Object;
				for(var i:int = 0;i<30;i++)
				{
					var img:Image = new Image();				
					p = photos.getItemAt(i);				
					img.source = "http://farm" + p.farm +
						".static.flickr.com/" + p.server +
						"/" + p.id + "_" + p.secret + ".jpg";				
					flickrImages.addItem(img);
				}
			}
			appModel.loadFlickrImages(flickrImages);
		}
		public function changeImageState(event:ChangeImageStateEvent):void
		{
			state = event.state;
			oldImageProperty = event.mouseProperty;
		}
		public function transformImage(event:TransformImageEvent):void
		{
			if (state == ImageManipulationState.MOVE)
			{		
				 appModel.currentSlide.x = oldImageProperty.x + event.mouseDelta.x;
				 appModel.currentSlide.y = oldImageProperty.y + event.mouseDelta.y;
			}		
			else if (state==ImageManipulationState.RESIZE)
			{
				appModel.currentSlide.scaleX =oldImageProperty.x - (event.mouseDelta.x/appModel.currentSlide.width);
				appModel.currentSlide.scaleY =oldImageProperty.y - (event.mouseDelta.y/appModel.currentSlide.height);
			}			
			else if (state==ImageManipulationState.ROTATE)
			{
				appModel.currentSlide.rotationZ = oldImageProperty.z - 180*(Math.atan2(event.mouseDelta.x,event.mouseDelta.y)/Math.PI) - 90;
			}
		}
		
	}
}

